package api

import (
	"github.com/gin-gonic/gin"
	"go-learn/dao"
	"go-learn/global"
	demo3 "go-learn/model/pb"
	demo2 "go-learn/model/request"
	"go-learn/model/response"
)

func InsertMany(c *gin.Context) {
	var data []demo3.User
	_ = c.ShouldBind(&data)
	err := dao.InsertMany(global.MongoCollection, data)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}
func FindById(c *gin.Context) {
	var data demo2.UserQuery
	_ = c.ShouldBind(&data)
	var res demo3.User
	err := dao.FindById(global.MongoCollection, data, &res)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(res, c)
}
func FindMany(c *gin.Context) {
	var data demo2.UserQuery
	_ = c.ShouldBind(&data)
	var res []demo3.User
	err := dao.FindMany(global.MongoCollection, data, &res)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(res, c)
}
func UpdateById(c *gin.Context) {
	var data demo3.User
	_ = c.ShouldBind(&data)
	err := dao.UpdateById(global.MongoCollection, data)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}
func UpdateMany(c *gin.Context) {
	var data demo2.UpdateMany
	_ = c.ShouldBind(&data)
	err := dao.UpdateMany(global.MongoCollection, data.Query, data.Update)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}
func DeleteById(c *gin.Context) {
	var data demo2.UserQuery
	_ = c.ShouldBind(&data)
	err := dao.DeleteById(global.MongoCollection, data.Id)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}
func DeleteMany(c *gin.Context) {
	var data demo2.UserQuery
	_ = c.ShouldBind(&data)
	err := dao.DeleteMany(global.MongoCollection, data)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}
